﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Routing;

namespace StockMatch.WebSite.Mvc
{
    public class DomainRoute : Route
    {
        private readonly string _domain = string.Empty;
        public string Domain
        {
            get { return _domain; }
            set { throw new NotImplementedException(); }
        }

        public DomainRoute(string domain, string url, IRouteHandler routeHandler)
            : base(url, routeHandler)
        {
            _domain = domain;
        }

        public DomainRoute(string domain, string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
            : base(url, defaults, routeHandler)
        {
            _domain = domain;
        }

        public DomainRoute(string domain, string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler)
            : base(url, defaults, constraints, routeHandler)
        {
            _domain = domain;
        }

        public DomainRoute(string domain, string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler)
            : base(url, defaults, constraints, dataTokens, routeHandler)
        {
            _domain = domain;
        }

        #region 以后在这里进行url重写
        public override RouteData GetRouteData(System.Web.HttpContextBase httpContext)
        {
            if (httpContext.Request.Url != null && (!string.IsNullOrEmpty(Domain) && httpContext.Request.Url.Host != Domain))
            {
                return null;
            }
            /*
            var virtualPath = httpContext.Request.Path;
            foreach (var url in UrlManager.CateUrls)
            {
                var regex = new Regex("^" + httpContext.Request.ApplicationPath.ResolveUrl(url.FormateUrl) + "$", RegexOptions.IgnoreCase);
                if (!regex.IsMatch(virtualPath)) continue;
                DomainRouteSection mainSite = DomainConfig.GetDomainRouteConfig(url.RouteName);
                this.Url = mainSite.url;
                this.DataTokens["namespaces"] = mainSite.Namespace;
                this.DataTokens["domain"] = mainSite.domain;
                this.DataTokens["area"] =mainSite.areas;
                
                var data = new RouteData(this, new MvcRouteHandler()); //声明一个RouteData，添加相应的路由值
                data.Values.Add("controller", url.RouteValues["controller"]);
                data.Values.Add("action", url.RouteValues["action"]);
                foreach (var value in url.RouteValues)
                {
                    if (value.Key != "controller" && value.Key != "action")
                    {
                        //data.Values.Add(value.Key,);
                    }
                }
                var keys = httpContext.Request.QueryString.AllKeys;
                foreach (var key in keys)
                {
                    data.Values.Add(key, httpContext.Request.QueryString[key]);
                }
                return data;
            }*/
            return base.GetRouteData(httpContext);
        }
        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            /*
            var controller = string.Empty;
            var action = string.Empty;

            if (values.ContainsKey("controller"))
            {
                controller = values["controller"].ToString();
            }
            if (values.ContainsKey("action"))
            {
                action = values["action"].ToString();
            }

            if (!string.IsNullOrEmpty(controller) && !string.IsNullOrEmpty(action))
            {

                var routes = UrlManager.CateUrls.Where(p => null != RouteTable.Routes[p.RouteName] &&
                                                            Equals(p.RouteValues["controller"], controller) &&
                                                            Equals(p.RouteValues["action"], action)).ToList();
               
                if (routes.Count > 0)
                {
                    var regex =
                        new Regex(
                            "^" + routes[0].RouteValues["pg"].ToString() + "$",
                            RegexOptions.IgnoreCase);


                    
                    var path =
                        requestContext.HttpContext.Request.ApplicationPath.ResolveUrl(routes[0].Url.Replace(routes[0].RouteValues["pg"].ToString(), values["pg"].ToString())).TrimStart('/');
                    return new VirtualPathData(this, path);
                }

            }*/
            return base.GetVirtualPath(requestContext, values);
        }

        #endregion
    }
}
